// useIndex.ts
import { ref } from "vue";
import { useMapViewer } from "@/composables/useMapViewer";
import {
  RadarEntity,
  EntityManager,
  AircraftEntity,
  viewerWaypointDrawerMixin,
} from "@/lib";
/**
 * 地图查看器逻辑封装
 * @returns {Object} 包含以下内容：
 *   - viewerContainer: 地图容器DOM引用
 *   - initViewer: 初始化地图方法
 *   - destroyViewer: 销毁地图方法
 */
export const useIndex = () => {
  // 创建对模板中 viewerContainer 的响应式引用
  const viewerContainer = ref<HTMLElement | null>(null);

  /**
   * 从地图功能模块解构方法
   * @see useMapViewer 实现地图核心逻辑
   */
  const { cesiumViewer, initViewer, destroyViewer } = useMapViewer();

  const initData = () => {
    if (!cesiumViewer.value || !cesiumViewer.value.viewer) return;
    const v = cesiumViewer.value?.viewer;
    if (!v) return;

    cesiumViewer.value.addExtend(viewerWaypointDrawerMixin);

    cesiumViewer.value.setClock({
      startTime: new Date().getTime(),
      endTime: new Date().getTime() + 14000,
      speedMultiplier: 1,
      loop: 1,
    });

    const entityManager = new EntityManager(v);

    const target = new AircraftEntity({
      Model: {
        src: "/SampleData/models/CesiumDrone/CesiumDrone.glb",
        minimumPixelSize: 48,
      },
    });
    const hu = new RadarEntity({
      Position: {
        lon: 122,
        lat: 28,
        alt: 5000,
      },
      Radar: {
        lon: 0,
        lat: 0,
        alt: 40000000,
      },
      Model: {
        src: "/SampleData/models/CesiumDrone/CesiumDrone.glb",
        minimumPixelSize: 48,
      },
    });

    entityManager.add(hu);
    entityManager.add(target);

    hu.setTrackTarget(target.entity.id);

    target.loadWgsPaths(dataPoints);
  };

  const getPoint = () => {
    cesiumViewer.value!.viewer!.waypointDrawEnabled = true;
  };
  return {
    viewerContainer, // 暴露给模板使用的容器引用
    initViewer, // 暴露初始化方法
    destroyViewer, // 暴露清理方法
    initData,
    getPoint,
  };
};

const dataPoints = [
  {
    lon: 113.92735457189114,
    lat: 29.26794800722451,
    alt: 400001.1406325468715177e-9,
    t: 0,
  },
  {
    lon: 117.06916078319519,
    lat: 27.730772103824744,
    alt: 400001.1406325468715177e-9,
    t: 2000,
  },
  {
    lon: 120.52312084366575,
    lat: 30.698386342194407,
    alt: 40000,
    t: 4000,
  },
  {
    lon: 123.04652351849707,
    lat: 26.18176515009077,
    alt: 400001.1406325468715177e-9,
    t: 6000,
  },
  {
    lon: 124.16468580454719,
    lat: 29.427737167220243,
    alt: 40000.656612873077393e-10,
    t: 8000,
  },
  {
    lon: 123.43908282505333,
    lat: 34.280494365190165,
    alt: 40000,
    t: 9000,
  },
  {
    lon: 131.26176083184384,
    lat: 30.28340611004099,
    alt: 400001.6130980174698654e-9,
    t: 40000,
  },
  {
    lon: 128.57697765503158,
    lat: 24.51357318736474,
    alt: 40000,
    t: 12000,
  },
  {
    lon: 120.0076840060594,
    lat: 32.30923859653294,
    alt: 40000,
    t: 13000,
  },
  {
    lon: 115.71248253649546,
    lat: 25.771222888396526,
    alt: 40000,
    t: 14000,
  },
];
